from django.shortcuts import render, redirect
from django.views.generic.base import View
from .models import Blog, Tags, Category
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from django.db.models import Q
from comments.models import Comment

# Create your views here.

class SearchView(View):
    def get(self, request, *args, **kwargs):
        keyword = request.GET.get('keyword', '')
        print(f'keyword的值为:{keyword}')
        if not keyword:
            return redirect('blog:index')
        blogs = Blog.objects.filter(Q(title__icontains=keyword) | Q(body__icontains=keyword))
        search_count = blogs.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'search.html', locals())


class IndexView(View):

    def get(self, request, *args, **kwargs):
        # 获取所有文章
        blogs = Blog.objects.all().order_by('-created')
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'index.html', locals())


class BlogDetailView(View):

    def get(self, request, id, *args, **kwargs):
        blog = get_object_or_404(Blog, id=id)
        blog.increase_views()
        comments = Comment.objects.filter(post=id).order_by('-created_time')
        print(comments)
        return render(request, 'blog.html', locals())


class TagsView(View):

    def get(self, request, *args, **kwargs):
        blogs = Blog.objects.order_by('-created')
        tags_blog = blogs.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'tags.html', locals())


class TagDetailView(View):

    def get(self, request, id, *args, **kwargs):
        blog = Tags.objects.get(id=id)
        blogs = blog.blog_set.order_by('-created')
        tags_blog = blogs.count()
        url_id = request.path.split('/')[-1]
        if int(url_id) == int(id):
            print('我是真')
            flag = True
        else:
            print('我是假')
            flag = False
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'tags.html', locals())


class TypesView(View):

    def get(self, request, *args, **kwargs):
        blogs = Blog.objects.order_by('-created')
        type_blog = blogs.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'types.html', locals())


class TypeDetailView(View):

    def get(self, request, id, *args, **kwargs):
        blog = Category.objects.get(id=id)
        blogs = blog.blog_set.order_by('-created')
        type_blog = blogs.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 5)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'types.html', locals())


class ArchivesView(View):

    def get(self, request, *args, **kwargs):
        blogs = Blog.objects.order_by('-created')
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        paginator = Paginator(blogs, 10)  # 每页显示5条
        page_obj = paginator.page(page)
        return render(request, 'archives.html', locals())


class AboutUsView(View):

    def get(self, request, *args, **kwargs):
        return render(request, 'about.html', locals())
